﻿function AzureTableQueryCombineExpressionModel(parentExpr, builderContext) {

    var self = this;

    var parent = ko.observable(parentExpr);

    var subexpressions = ko.observableArray([]);

    var operator = ko.observable();

    var queryString = ko.computed(function () {
        var res = '';
        var op = operator();
        var children = subexpressions();
        var cnt = 0;
        for (var i = 0; i < children.length; i++) {
            var child = children[i];
            var childQuery = child.queryString();
            if (childQuery) {
                if (cnt) res += ' ' + op + ' ';
                res += childQuery;
                cnt++;
            }
        }
        if (cnt > 1) {
            return '(' + res + ')';
        } else {
            return res;
        }
    });

    var queryHtmlString = ko.computed(function () {
        var res = '';
        var op = operator();
        var children = subexpressions();
        var cnt = 0;
        for (var i = 0; i < children.length; i++) {
            var child = children[i];
            var childQuery = child.queryHtmlString();
            if (childQuery) {
                if (cnt) res += ' ' + op + ' ';
                res += childQuery;
                cnt++;
            }
        }
        if (builderContext.highlightedExpression() == self) {
            res = '<span class="highlighted">' + res + '</span>';
        }
        if (cnt > 1) {
            return '(' + res + ')';
        } else {
            return res;
        }
    });

    var canRemove = ko.computed(function () {
        return true;
    });

    var canAdd = ko.computed(function () {
        return true;
    });

    function invert() {
        var res = new AzureTableQueryFuncExpressionModel(parent(), builderContext);
        res.funcName('not');
        res.arg(self);
        parent().replaceChild(self, res);
    }

    function removeChild(child) {
        subexpressions.remove(child);
        var list = subexpressions();
        if (list.length == 1) {
            var inner = list[0];
            parent().replaceChild(self, inner);
        }
    }

    function replaceChild(oldExpression, newExpression) {
        subexpressions.replace(oldExpression, newExpression);
        newExpression.parent(self);
    }

    function addChild(newExpression) {
        subexpressions.push(newExpression);
        newExpression.parent(self);
    }

    function addExpression() {
        subexpressions.push(new AzureTableQueryCompareExpressionModel(self, builderContext));
    }

    function highlight() {
        builderContext.highlightedExpression(self);
    }

    self.type = 'combine';

    self.parent = parent;
    self.subexpressions = subexpressions;
    self.operator = operator;

    self.canRemove = canRemove;
    self.canAdd = canAdd;
    self.removeExpr = removeChild;
    self.invert = invert;
    self.replaceChild = replaceChild;
    self.addChild = addChild;
    self.addExpression = addExpression;
    self.highlight = highlight;
    self.queryString = queryString;
    self.queryHtmlString = queryHtmlString;
}